package cn.jilian.meta.handler.generators;

import java.sql.Types;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import cn.jilian.common.config.SpringBeans;
import cn.jilian.entity.MField;
import cn.jilian.entity.Meta;
import cn.jilian.meta.handler.CompGenerator;
import cn.jilian.meta.handler.GeneratorManager;
import cn.jilian.meta.handler.domain.Form;
import cn.jilian.meta.handler.domain.FormItem;
import cn.jilian.meta.handler.domain.FuncCallExpression;
import cn.jilian.meta.handler.domain.Function;
import cn.jilian.meta.handler.domain.SimpleRestJsonStore;
import cn.jilian.meta.service.MetaService;

public class SimpleFormGenerator implements CompGenerator<Form> {

	private Function defaultSubmitFunction;
	
	private Map<String,Object> defaultSubmitButtonConfig;
	
	
	private MetaService metaService;
	
	private CompGenerator<SimpleRestJsonStore> simpleRestJsonStoreGenerator;
	
	
	public SimpleFormGenerator(GeneratorManager manager) {
		this.defaultSubmitFunction=new Function("","var form = this.up('form').getForm();\n" + 
				"            if (form.isValid()) {\n" + 
				"                form.submit({\n" + 
				"                    success: function(form, action) {\n" + 
				"                       Ext.Msg.alert('成功', action.result.msg);\n" + 
				"                    },\n" + 
				"                    failure: function(form, action) {\n" + 
				"                        Ext.Msg.alert('失败', action.result.msg);\n" + 
				"                    }\n" + 
				"                });\n" + 
				"            }");
		this.defaultSubmitButtonConfig=new HashMap<>();
		this.defaultSubmitButtonConfig.put("text", "提交");
		this.defaultSubmitButtonConfig.put("handler", defaultSubmitFunction);
		this.defaultSubmitButtonConfig.put("formBind", true);
		this.metaService=SpringBeans.getBean(MetaService.class);
		this.simpleRestJsonStoreGenerator=manager.getGenerator(SimpleRestJsonStore.class);
	}
	
	@Override
	public Form convert(Meta meta, Map<String, Object> extConfig) {
		Form form=new Form();
		Map<String,Object> defaults=new HashMap<>();
		defaults.put("xtype", "textfield");
		defaults.put("width", 100);
		form.setDefaults(defaults);
		List<MField> fs=meta.getFields();
		Collection<Object> items=new ArrayList<>();
		for(MField f:fs) {
			Map<String,Object> item=new HashMap<>();
			String text=f.getText();
			Integer type=f.getType();
			if(f.getAttr(MField.REQUIRED)!=null) {
				item.put("allowBlank", false);
			}
			items.add(item);
			item.put("fieldLabel", text);
			item.put("name", f.getName());
			if(f.containsAttr(MField.PICKER_NAME)) {
				String picker=(String) f.getAttr(MField.PICKER_NAME);
				if(StringUtils.equalsIgnoreCase(picker, FormItem.COLOR_PICKER)) {
					item.put("xtype", FormItem.COLOR_PICKER);
				}else if(StringUtils.equalsIgnoreCase(picker, FormItem.MONTH)) {
					item.put("xtype", FormItem.MONTH);
				}else if(StringUtils.equalsIgnoreCase(picker, FormItem.TREE)){
					item.put("xtype", FormItem.TREE);
				}else {
					item.put("xtype", "pickerfield");
				}
			}
			
			if(f.containsAttr(MField.ENUMS)) {
				String enumCode=(String) f.getAttr(MField.ENUMS);
				enumCode=StringUtils.substringAfterLast(enumCode, "$");
				Meta enumMeta=metaService.queryMetaByName(enumCode);
				SimpleRestJsonStore store=simpleRestJsonStoreGenerator.convert(enumMeta, null);
				item.put("xtype", FormItem.COMBO);
				item.put("valueField", f.getAttr(MField.VALUEFIELD, "code"));
				item.put("displayField", f.getAttr(MField.DISPLAYFIELD, "text"));
				item.put("queryMode", "local");
				item.put("triggerAction", "all");
				item.put("store", new FuncCallExpression("Ext.create",store));
			}
			switch(type) {
			case Types.CHAR:
				item.put("xtype", FormItem.TEXTFIELD);
				break;
			case Types.BIGINT:
				item.put("xtype", FormItem.NUMBER);
				break;
			case Types.BOOLEAN:
				item.put("xtype", FormItem.RADIOGROUP);
				item.put("cls", "x-check-group-alt");
				Collection<Map<String,Object>> cs=new ArrayList<>();
				Map<String,Object> g1=new HashMap<>();
				g1.put("boxLabel", "开启");
				g1.put("inputValue", Boolean.TRUE);
				cs.add(g1);
				Map<String,Object> g2=new HashMap<>();
				g2.put("boxLabel", "禁用");
				g2.put("inputValue", Boolean.FALSE);
				cs.add(g2);
				item.put("items", cs);
				break;
			case Types.BLOB:
				item.put("xtype", FormItem.FILE);
				
				break;
			case Types.ARRAY:
				item.put("xtype", FormItem.TAGFIELD);
				
				break;
			}
			
			
		}
		form.setItems(items);
		Collection<Object> buttons=new HashSet<>();
		buttons.add(defaultSubmitButtonConfig);
		form.setButtons(buttons);
		return form;
	}

}
